package g11.testdao;

import g11.dao.DAOException;
import g11.dao.UserDAO;
import g11.dao.UserDAOImpl;
import g11.model.User;
import junit.framework.TestCase;

import org.junit.Test;

public class testUserDao extends TestCase {
	@Test
	public void testDeleteUser() throws DAOException {
		User user = new User();
		user.setUsername("benny");
		user.setPassword("1234");
		Byte type = 1;
		user.setType(type);
		user.setName("Benny");
		UserDAO userDao = new UserDAOImpl();
		userDao.createUser(user);
		User userRetrieved = new User();
		userRetrieved = userDao.getUserbyUsername("benny", "1234", type);
		assertNotNull(userRetrieved);
		userDao.deleteUser(userRetrieved);
		userRetrieved = userDao.getUserbyUsername("benny", "1234", type);
		assertNull(userRetrieved);
	}

	@Test
	public void testCreateUser() throws DAOException {
		User user = new User();
		user.setUsername("steve");
		user.setPassword("1234");
		Byte type = 1;
		user.setType(type);
		user.setName("Steve Jobs");
		UserDAO userDao = new UserDAOImpl();
		userDao.createUser(user);
		User userRetrieved = new User();
		userRetrieved = userDao.getUserbyUsername("steve", "1234", type);
		assertNotNull(userRetrieved);
		assertEquals("Steve Jobs", userRetrieved.getName());
		userDao.deleteUser(userRetrieved);

	}

	@Test
	public void testUpdateUser() throws DAOException {
		UserDAO userDao = new UserDAOImpl();
		User user = new User();
		user.setUsername("star");
		user.setPassword("1234");
		Byte type = 1;
		user.setType(type);
		user.setName("Starbuck");
		userDao.createUser(user);
		User userRetrieved = new User();
		userRetrieved = userDao.getUserbyUsername("star", "1234", type);
		assertNotNull(userRetrieved);
		assertEquals("Starbuck", userRetrieved.getName());
		userRetrieved.setName("Tim Horton");
		userDao.updateUser(userRetrieved);
		userRetrieved = userDao.getUserbyUsername("star", "1234", type);
		assertEquals("Tim Horton", userRetrieved.getName());
		userDao.deleteUser(userRetrieved);
	}

}
